<?php

namespace app\admin\controller;
use think\Db;
use think\Controller;
use think\Request;
use think\Session;


class Role extends AdminController
{
    public function _initialize(){
        parent::_initialize();
    }
    public function index()
    {
        // 读取用户列表
        $data =Db::table('sm_a_role')
                ->alias('sar')
                ->join('sm_a_role_power sarp', 'sarp.rid  = sar.id')
                ->join('sm_a_power sap', 'sap.id = sarp.pid')
                ->field('sar.name,sap.name as sapname,sar.id')
                ->order('sar.id')
                // ->paginate(5);
                ->select();
        $res = Db::name('a_role')->field(['id', 'name','status','remark'])->order('id', 'ASC')->paginate(5);
        $page = $res->render();
        // var_dump($data);
        // var_dump($res);
        return view('admin@Role/index',[
            'data' => $data,
            'title' => '我是显示角色页面',
            'res' => $res,
            'page'=> $page,
        ]);
    }

    public function add()
    {

        return view('admin@Role/add',[
            'title' => '我是添加角色页面',
        ]);
    }
    // 添加角色
    public function insert(Request $request)
    {
        $data = $request->post();
        $d['Name'] = $data['Name'];
        $d['status'] = '0';
        $d['Remark'] = $data['Remark'];

        $s = [
            'name' => $d['Name'],
            'status' => $d['status'],
            'remark' => $d['Remark'],

        ];
        $result = Db('a_role')->insert($s);

        $rid = Db::name('a_role')->field('id')->where('name', $d['Name'])->find();
        $d = [
            'rid' => $rid['id'],
            'pid' => '1',
        ];
        $list = db('a_role_power')->insert($d);

        if ($result) {
            return $this->success('新增成功');
        } else {
            return $this->error('新增失败');
        }
    }

    public function delete($id)
    {
        // echo $id;
       $result = db('a_role')->delete($id);
       if ($result > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为' . $id . '的用户删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为' . $id . '的用户删除失败!';
        }
        return json($info);
    }

    // 修改角色
        public function edit($id)
    {
        $data = db('a_role')->find($id);
        return view('admin@Role/edit',[
            'title' => '我是修改ID:'.$id.'角色页面',
            'data' => $data,
            'id' => $id
        ]);
    }

        public function update(Request $request)
    {
        $data = $request->post();
        // var_dump($data);die;
        $id = $data['id'];
        $a = [
            'name' => $data['Name'],
            'remark' => $data['Remark'],
            'status' => $data['go'],
            ];

        $result = Db::name('a_role')->where('id',$id)->update($a);
        if ($result) {
            return $this->success('编辑成功');
        } else {
            return $this->error('编辑失败');
        }
    }


    // 分配角色
    public function getPower($id)
    {
       
        $input = input();
        $rid = $input['id'];
        $list = Db::table('sm_a_role')
            ->alias('sar')
            ->join('sm_a_role_power sarp', 'sarp.rid  = sar.id')
            ->join('sm_a_power sap', 'sap.id = sarp.pid')
            ->field('sap.name as sapname,sap.id as sapid')
            ->where('sar.id',$rid)
            ->select();
        // $rname = $list[0];
        $role = Db::name('a_power')->field(['id', 'name',])->order('id', 'ASC')->select();
        foreach($list as $v){
            $myrole[] = $v['sapid'];
        }
            // var_dump($list);
        // var_dump($myrole);die;
        // var_dump($role);die;
        return view('admin@Role/choosePower', [
            'role' => $role,
            'list' => $myrole,
            'rid' => $rid,
            'title' => '我是ID:'.$rid.'角色分配节点页面',
        ]);

    }

    public function updatePower(Request $request)
    {
        $data = $request->post();
        // var_dump($data);die;
        $id = $data['rid'];
        // 删除原来节点
        $r = Db::name('a_role_power')->where('rid',$id)->delete();


        foreach ($data['power'] as $k => $v) {
            $a = [
                'pid' => $v,
                'rid' => $id,
            ];
            // var_dump($a);
            $result = Db::name('a_role_power')->where('rid',$id)->insert($a);
        }
        


        if ($result) {
            return $this->success('分配节点成功');
        } else {
            return $this->error('分配节点失败');
        }
    }



}
